package com.woniu.daiquhua.utils;

import com.woniu.daiquhua.entity.RepaymentPlan;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class RepaymentplanUtil {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    /**
     * Methad info:等额本息
     * @author 流光亦断
     * @date 2021/9/15 15:01
     * @return java.util.List<com.woniu.daiquhua.entity.RepaymentPlan>
     */
    public List<RepaymentPlan> Equalinstallmentsofprincipalandinterest(int repayDate, BigDecimal rate, BigDecimal loadMoney, int planNum){
        /*
         * 已知数据，通过数据库查询
         */
        //根据uid查出该用户的还款日repayDate和利率rate
       /* int repayDate=3;//还款日子数
        BigDecimal rate=new BigDecimal(0.0005);
        //根据借据号查出借款金额loadMoney和借款日cal，总期数snum
        BigDecimal loadMoney=new BigDecimal(1500);*/
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
       /* int snum=3;*/

        /*
         * 计算部分1，判断还款月份
         */
        int loadDate = cal.get(Calendar.DAY_OF_MONTH);//借款日子数
        int loadDays=cal.getActualMaximum(Calendar.DAY_OF_MONTH);//借款当月的总天数
        Calendar reCal=Calendar.getInstance();//第一期还款日期
        reCal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),repayDate); //第一期还款日期，初始化为当月
        if (loadDate<=repayDate){
            if (repayDate-loadDate<20){//第一期下月还
                reCal.add(Calendar.MONTH,1);
            }
        }else {
            if ((loadDays-loadDate+repayDate)>=20){//第一期下月还
                reCal.add(Calendar.MONTH,1);
            }else {//第一期下下月还
                reCal.add(Calendar.MONTH,2);
            }
        }



        /*
         *计算部分2，计算月供(每期应还的本金+利息之和),每月本金，利息
         */
        BigDecimal monthRate=rate.multiply(new BigDecimal(360)).divide(new BigDecimal(12));//月利率
        BigDecimal money= loadMoney.multiply(monthRate).multiply((new BigDecimal(1).add(monthRate)).pow(planNum)).divide(((new BigDecimal(1).add(monthRate)).pow(planNum).subtract(new BigDecimal(1))),5,BigDecimal.ROUND_HALF_UP).setScale(5, RoundingMode.HALF_UP);//保留两位小数;
        RepaymentPlan rp=null;
        List<RepaymentPlan> list=new ArrayList<>();
        BigDecimal remainPrincipal=loadMoney;//剩余本金
        for (int i = 1; i <= planNum; i++) {
            rp=new RepaymentPlan();
            BigDecimal interest=new BigDecimal(0);
            BigDecimal principal=new BigDecimal(0);
            if (i==1){

                //还款时间
                rp.setpRepaymentTime(format.format(reCal.getTime()));
            }else {
                //还款时间
                reCal.add(Calendar.MONTH,1);
                rp.setpRepaymentTime(format.format(reCal.getTime()));
            }
            //计算利息
            interest=remainPrincipal.multiply(monthRate).setScale(5, RoundingMode.HALF_UP);//保留两位小数;
            rp.setpInterest(interest);
            //计算本金
            principal=money.subtract(interest).setScale(5, RoundingMode.HALF_UP);//保留两位小数;
            rp.setpPrincipal(principal);
            //封装月供
            rp.setpMoney(money);
            //封装期数
            rp.setpMnumber(i);
            //计算剩余本金
            remainPrincipal=remainPrincipal.subtract(principal).setScale(5, RoundingMode.HALF_UP);//保留两位小数;
            list.add(rp);
        }

        return list;

    }



    public List<RepaymentPlan> equalprincipal(int repayDate, BigDecimal rate, BigDecimal loadMoney, int planNum){
        /*
         * 已知数据，通过数据库查询
         */
        //根据uid查出该用户的还款日repayDate和利率rate
    /*    int repayDate=3;//还款日子数
        BigDecimal rate=new BigDecimal(0.0005);
        //根据借据号查出借款金额loadMoney和借款日cal，总期数snum
        BigDecimal loadMoney=new BigDecimal(1500);*/
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        /*int snum=3;*/

        /*
         * 计算部分1，判断还款月份
         */
        int loadDate = cal.get(Calendar.DAY_OF_MONTH);//借款日子数
        int loadDays=cal.getActualMaximum(Calendar.DAY_OF_MONTH);//借款当月的总天数
        Calendar reCal=Calendar.getInstance();//第一期还款日期
        reCal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),repayDate); //第一期还款日期，初始化为当月
        if (loadDate<=repayDate){
            if (repayDate-loadDate<20){//第一期下月还
                reCal.add(Calendar.MONTH,1);
            }
        }else {
            if ((loadDays-loadDate+repayDate)>=20){//第一期下月还
                reCal.add(Calendar.MONTH,1);
            }else {//第一期下下月还
                reCal.add(Calendar.MONTH,2);
            }
        }

        /*
         *计算部分2，计算月供(每期应还的本金+利息之和),每月本金，利息
         */
        BigDecimal monthRate=rate.multiply(new BigDecimal(360)).divide(new BigDecimal(12));//月利率
        //每月本金
        BigDecimal principal= loadMoney.divide(new BigDecimal(planNum),5,BigDecimal.ROUND_HALF_UP).setScale(5, RoundingMode.HALF_UP);//保留两位小数;
        RepaymentPlan rp=null;
        List<RepaymentPlan> list=new ArrayList<>();
        BigDecimal remainPrincipal=loadMoney;//剩余本金
        for (int i = 1; i <= planNum; i++) {
            rp=new RepaymentPlan();
            BigDecimal interest=new BigDecimal(0);
            BigDecimal money=new BigDecimal(0);
            if (i==1){
                
                //还款时间
                rp.setpRepaymentTime(format.format(reCal.getTime()));
            }else {
                //还款时间
                reCal.add(Calendar.MONTH,1);
                rp.setpRepaymentTime(format.format(reCal.getTime()));
            }
            //计算利息
            interest=remainPrincipal.multiply(monthRate).setScale(5, RoundingMode.HALF_UP);//保留两位小数;
            rp.setpInterest(interest);
            //计算月供
            money=principal.add(interest).setScale(5, RoundingMode.HALF_UP);//保留两位小数;
            rp.setpMoney(money);
            //封装本金
            rp.setpPrincipal(principal);
            //封装期数
            rp.setpMnumber(i);
            //计算剩余本金
            remainPrincipal=remainPrincipal.subtract(principal).setScale(5, RoundingMode.HALF_UP);//保留两位小数;
            list.add(rp);
        }

        return list;
    }



}
